package com.itsu.spbspider.service.impl;

import com.alibaba.fastjson.JSON;
import com.itsu.spbspider.entity.source.SourceTouTiao;
import com.itsu.spbspider.entity.source.SourceTouTiaoContent;
import com.itsu.spbspider.entity.source.SourceTouTiaoVideo;
import com.itsu.spbspider.exception.SystemException;
import com.itsu.spbspider.util.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author suben
 * @create time 2020/2/13 22:21
 */
@Service
@Slf4j
public class ToutiaoVideoCrawler {

    public Map crawlerData() throws Exception{
        Map map = null;
        try {
            map = new HashMap();
            String content = HttpUtil.get("https://is-lq.snssdk.com/api/news/feed/v47/?category=video&refer=1&refresh_reason=1&count=20&min_behot_time=1581599529&last_refresh_sub_entrance_interval=1581600290&loc_mode=7&tt_from=pull&lac=9779&cid=160850497&cp=53ea465d46e22q1&plugin_enable=4&client_extra_params=%7B%7D&last_ad_show_interval=7&cached_item_num=0&iid=103308864183&device_id=59298520278&ac=wifi&mac_address=F8%3AC3%3A9E%3AF3%3ABB%3ADB&channel=lite_huawei&aid=35&app_name=news_article_lite&version_code=733&version_name=7.3.3&device_platform=android&ab_version=668905%2C668907%2C808414%2C772539%2C668908%2C668904%2C668906%2C1466732%2C668903%2C1454796%2C1259055%2C928942%2C1454824%2C1454823%2C1454833&ab_client=a1%2Cc4%2Ce1%2Cf2%2Cg2%2Cf7&ab_group=z1&ab_feature=z1&abflag=3&ssmix=a&device_type=COL-AL10&device_brand=HONOR&language=zh&os_api=29&os_version=10&openudid=af645d0a2e415da9&manifest_version_code=733&resolution=1080*2190&dpi=480&update_version_code=73307&_rticket=1581600290200&sa_enable=0&lite_qi_enable=1&plugin_state=2396217375&tma_jssdk_version=1.49.0.6&rom_version=29&cdid=3fa2ad2b-67ac-48bb-9bbd-0ff1bad90884&oaid=faffd7ff-dbdf-0fca-bfc7-b97bff9b4dab");
            log.info(content);
            List<Map> list = new ArrayList<>();
            SourceTouTiao sourceTouTiao = JSON.parseObject(content).toJavaObject(SourceTouTiao.class);
            for (SourceTouTiao.DataBean dataBean : sourceTouTiao.getData()) {
                SourceTouTiaoContent sourceTouTiaoContent = JSON.parseObject(dataBean.getContent()).toJavaObject(SourceTouTiaoContent.class);
                if (sourceTouTiaoContent.getVideo_play_info() == null)
                    continue;
                Map ret = new HashMap();
                String title = sourceTouTiaoContent.getTitle();
                int publish_time = sourceTouTiaoContent.getPublish_time();
                int read_count = sourceTouTiaoContent.getRead_count();
                SourceTouTiaoVideo sourceTouTiaoVideo = JSON.parseObject(sourceTouTiaoContent.getVideo_play_info()).toJavaObject(SourceTouTiaoVideo.class);
                String main_url_encode = sourceTouTiaoVideo.getVideo_list().getVideo_1().getMain_url();
                String mainUrl = decode(main_url_encode);
                String poster_url = sourceTouTiaoVideo.getPoster_url();
                ret.put("text", title);
                ret.put("passtime", this.formatDate(publish_time, "MM-dd HH:mm"));
                ret.put("playcount", read_count);
                ret.put("videoUrl", mainUrl);
                ret.put("image", poster_url);
                list.add(ret);
            }
            map.put("data", list);
        } catch (Exception e) {
            throw new SystemException(e);
        }

        return map;
    }

    public String decode(String source) {
        byte[] bytes = Base64.decodeBase64(source);
        return new String(bytes);
    }

    public String formatDate(Object source, String format) {
        return new SimpleDateFormat(format).format(source);
    }
}
